package com.hongyuan.sellfitness.ui.home.view_mode;

import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.hongyuan.mvvmhabitx.binding.command.BindingAction;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.my_view.scllor_view.UnitBeanUtils;
import com.hongyuan.sellfitness.ui.home.activity.BuyLessonActivity;
import com.hongyuan.sellfitness.ui.home.beans.GroupSiteBean;
import com.hongyuan.sellfitness.ui.home.beans.StoreGroupDetailBean;
import com.hongyuan.sellfitness.ui.main.activity.SuccessActivity;
import com.hongyuan.sellfitness.ui.main.beans.V3SuccessBeans;
import com.hongyuan.sellfitness.ui.person.beans.KeFuBean;

import java.util.ArrayList;
import java.util.List;

public class SignUpGroupViewModel extends CustomViewModel<MyRepository> {

    //配送方式选择工具类
    private UnitBeanUtils rUtils;

    public ObservableField<StoreGroupDetailBean> entity = new ObservableField<>();

    public ObservableField<KeFuBean> callKfTel = new ObservableField<>();

    public SignUpGroupViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //显示滚动弹框
        public SingleLiveEvent<UnitBeanUtils> showSelectSite = new SingleLiveEvent<>();
        //设置位置数据
        public SingleLiveEvent<List<Integer>> setSites = new SingleLiveEvent<>();
        //报名团课
        public SingleLiveEvent<Void> goNext = new SingleLiveEvent<>();
    }

    //选择位置
    public BindingCommand selectSite = new BindingCommand(() -> {
        uc.showSelectSite.setValue(rUtils);
    });
    //报名团课
    public BindingCommand goNext = new BindingCommand(() -> {
        uc.goNext.call();
    });

    //拨打电话
    public BindingCommand callTel = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            getUC().getCallTel().setValue(callKfTel.get().getInfo().getTel());
        }
    });

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_SOURSE_SUPER_INFO){
            StoreGroupDetailBean bean = (StoreGroupDetailBean)dataBean;
            entity.set(bean);
        }

        if(code == Constants.GET_CS_NUMBER){
            List<Integer> list = ((GroupSiteBean)dataBean).getList();

            rUtils = new UnitBeanUtils<Integer>() {
                @Override
                public String unit(Integer o) {
                    return String.valueOf(o);
                }

                @Override
                public String unit_cn(Integer o) {
                    return String.valueOf(o);
                }
            };

            uc.setSites.setValue(list);
        }

        if(code == Constants.GET_ONLINE_KF){
            KeFuBean keFuBean = (KeFuBean)dataBean;
            callKfTel.set(keFuBean);
        }

        if(code == Constants.SIGIN_UP_COURSE_SUPER){

            V3SuccessBeans beans = new V3SuccessBeans();
            beans.setType(V3SuccessBeans.TYPE.GROUPCLASS);
            beans.setTitleText("团课报名");
            beans.setShowText("预约成功");
            beans.setBtn1Text("取消报名");
            beans.setBtn2Text("完成");
            List<V3SuccessBeans.ItemConten> list = new ArrayList<>();

            V3SuccessBeans.ItemConten itemConten = new V3SuccessBeans.ItemConten();
            itemConten.setContent(entity.get().getCs_name());
            itemConten.setItemTitle("课程名：");
            list.add(itemConten);

            itemConten = new V3SuccessBeans.ItemConten();
            itemConten.setContent(entity.get().getOs_name());
            itemConten.setItemTitle("上课场地:");
            list.add(itemConten);

            itemConten = new V3SuccessBeans.ItemConten();
            itemConten.setContent(entity.get().getCs_start_date());
            itemConten.setItemTitle("上课开始时间:");
            list.add(itemConten);

            beans.setItemContens(list);

            Bundle bundle = new Bundle();
            bundle.putSerializable("successBeans",beans);
            startActivity(SuccessActivity.class,bundle);
            finish();
        }
    }
}
